﻿using HalconDotNet;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using thinger.VisionModels;

namespace thinger.VisitionDAL
{
   
    public class CalibrationHelper
    {
        /// <summary>
        /// 获取所有的标定数据
        /// </summary>
        /// <param name="basePath"></param>
        /// <returns></returns>
        public List<string> GetAllCalibrations(string basePath)
        {
            List<string> result = new List<string>();

            DirectoryInfo directoryInfo = new DirectoryInfo(basePath);

            foreach (var item in directoryInfo.GetDirectories())
            {
                result.Add(item.Name);
            }
            return result;
        }

        /// <summary>
        /// 标定
        /// </summary>
        /// <param name="calibrationDatas"></param>
        /// <returns></returns>
        public OperateResult<HTuple> Calibration(List<CalibrationData> calibrationDatas)
        {
            try
            {
                HTuple ImageX = new HTuple();
                HTuple ImageY = new HTuple();
                HTuple MechanicalX = new HTuple();
                HTuple MechanicalY = new HTuple();

                HTuple homMat2D = new HTuple();

                foreach (var item in calibrationDatas)
                {
                    ImageX = ImageX.TupleConcat(item.ImageX);
                    ImageY = ImageY.TupleConcat(item.ImageY);
                    MechanicalX = MechanicalX.TupleConcat(item.PositionX);
                    MechanicalY = MechanicalY.TupleConcat(item.PositionY);
                }

                HOperatorSet.VectorToHomMat2d(ImageX, ImageY, MechanicalX, MechanicalY, out homMat2D);

                return OperateResult.CreateSuccessResult(homMat2D);

            }
            catch (Exception ex)
            {
                return OperateResult.CreateFailResult<HTuple>("标定失败：" + ex.Message);
            }
        }

        /// <summary>
        /// 保存标定信息
        /// </summary>
        /// <param name="dir"></param>
        /// <param name="calirationInfo"></param>
        /// <returns></returns>
        public OperateResult SaveCalibration(string dir, CalibrationInfo calirationInfo)
        {
            bool result = IniConfigHelper.WriteIniData("标定数据", "九点标定", JsonHelper.EntityToJSON(calirationInfo.CalibrationDatas), dir + "\\CalibrationData.ini");

            if (result == false)
            {
                return OperateResult.CreateFailResult("九点标定数据写入失败");
            }

             result = IniConfigHelper.WriteIniData("标定数据", "位置信息", JsonHelper.EntityToJSON(calirationInfo.PositionInfo), dir + "\\Position.ini");

            if (result == false)
            {
                return OperateResult.CreateFailResult("九点标定数据写入失败");
            }

            try
            {
                HOperatorSet.WriteTuple(calirationInfo.HomMat2D, dir + "\\HomMat2D.tup");
            }
            catch (Exception ex)
            {
                return OperateResult.CreateFailResult("仿真变换矩阵写入失败：" + ex.Message);
            }
            return OperateResult.CreateSuccessResult();
        }

        public OperateResult<CalibrationInfo> LoadCalibration(string dir)
        {
            CalibrationInfo calibrationInfo = new CalibrationInfo();

            calibrationInfo.CalibrationDatas = JsonHelper.JSONToEntity<List<CalibrationData>>(IniConfigHelper.ReadIniData("标定数据", "九点标定", "", dir + "\\CalibrationData.ini"));
            calibrationInfo.PositionInfo = JsonHelper.JSONToEntity<PositionInfo>(IniConfigHelper.ReadIniData("标定数据", "位置信息", "", dir + "\\Position.ini"));
            try
            {
                HTuple homMat2D;

                HOperatorSet.ReadTuple(dir + "\\HomMat2D.tup", out homMat2D);

                calibrationInfo.HomMat2D = homMat2D;
            }
            catch (Exception ex)
            {
                return OperateResult.CreateFailResult<CalibrationInfo>("仿真变换矩阵读取失败：" + ex.Message);
            }

            return OperateResult.CreateSuccessResult(calibrationInfo);
        }

    }
}
